#include "testgame.h"
#include "knightsprite.h"
#include "all_gfx.h"

TestGame::TestGame()
        :RpgGame()
{
    spritePalette = new RpgSpritePal(SCREEN_TOP, nextPalId(), (void *) knight_Pal);
    knightSprite = new KnightSprite(SCREEN_TOP, nextSpriteId(), spritePalette);
    bg = new RpgBackground(SCREEN_TOP, nextBackgroundId(), (PA_BgStruct *) &testmap);
}

void TestGame::init()
{
    PA_Init();
    PA_InitVBL();

    bg->load();

    spritePalette->load();
    knightSprite->create();
    knightSprite->setSpriteDown();
}

void TestGame::run()
{
    while(true)
    {
        knightSprite->checkInput();
        knightSprite->updatePosition();

        checkScroll();

        PA_WaitForVBL();
    }
}

void TestGame::checkScroll()
{
    if (norm(knightSprite->xf() - knightSprite->scrollXf()) > 160
        && knightSprite->x() < 1280)
    {
        knightSprite->setScrollPositionXf(knightSprite->xf() - fix(160));
    }
    else if (norm(knightSprite->xf() - knightSprite->scrollXf()) < 64
             && knightSprite->x() > 64)
    {
        knightSprite->setScrollPositionXf(knightSprite->xf() - fix(64));
    }

    bg->scrollX(knightSprite->scrollX());

    if (norm(knightSprite->yf() - knightSprite->scrollYf()) > 96
        && knightSprite->y() < 2560)
    {
        knightSprite->setScrollPositionYf(knightSprite->yf() - fix(96));
    }
    else if (norm(knightSprite->yf() - knightSprite->scrollYf()) < 32
             && knightSprite->y() > 32)
    {
        knightSprite->setScrollPositionYf(knightSprite->yf() - fix(32));
    }

    bg->scrollY(knightSprite->scrollY());
}

TestGame::~TestGame()
{
    if (spritePalette != 0) delete spritePalette;
    if (knightSprite != 0) delete knightSprite;
    if (bg != 0) delete bg;
}
